//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.TridentModelSL
{
    using System;
    using service = Microsoft.Research.ScientificWorkflow.TridentSilverlightService;

    /// <summary>
    /// Represents available user roles
    /// </summary>
    public enum UserRole
    {
        /// <summary>
        /// Admin role
        /// </summary>
        Admin,

        /// <summary>
        /// Normal user role
        /// </summary>
        /// 
        NormalUser,

        /// <summary>
        /// Guest user
        /// </summary>
        Guest,

        /// <summary>
        /// Service user
        /// </summary>
        Service,

        /// <summary>
        /// Invalid user
        /// </summary>
        Invalid
    }

    /// <summary>
    /// Represents a User object for the UI elements
    /// </summary>
    public class UserModel
    {
        private UserRole role;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserModel"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        public UserModel(service.UserModel source)
        {
            this.UserId = source.id;
            this.UserName = source.name;
            try
            {
                this.Role = (UserRole)Enum.Parse(typeof(UserRole), source.role.ToString(), true);
            }
            catch (Exception)
            {
                this.Role = UserRole.Invalid;
            }
        }

        /// <summary>
        /// Convert to network model.
        /// </summary>
        /// <returns></returns>
        public service.UserModel ToNetwork()
        {
            service.UserModel user = new service.UserModel();
            user.id = this.UserId;
            user.name = this.UserName;
            user.role = (service.UserRole)Enum.Parse(typeof(service.UserRole), this.Role.ToString(), true);

            return user;
        }

        /// <summary>
        /// Gets or sets the role.
        /// </summary>
        /// <value>The role.</value>
        public UserRole Role
        {
            get
            {
                return role;
            }

            set
            {
                role = value;
            }
        }

        /// <summary>
        /// Gets or sets the user id.
        /// </summary>
        /// <value>The user id.</value>
        public Guid UserId { get; private set; }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName { get; private set; }
    }
}
